﻿@namespace Gurux.DLMS.AMI.Components.Tab
@using System.Diagnostics

<div style="position: relative; width: 100%; ">
    @if (Parent?.ActivePage == this)
    {
        @ChildContent
    }
</div>

@code {
    [CascadingParameter]
    private TabControl? Parent { get; set; }

    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    ///<summary>
    ///Tab page path.
    ///</summary>
    [Parameter]
    public string? Path { get; set; }

    ///<summary>
    ///Tab page header text.
    ///</summary>
    [Parameter]
    public string Text { get; set; } = "";

    ///<summary>
    ///Icon class
    ///</summary>
    [Parameter]
    public string? Icon { get; set; }

    ///<summary>
    ///Icon ligature.
    ///</summary>
    [Parameter]
    public string? IconLigature { get; set; }

    [Parameter]
    public bool Active { get; set; }

    ///<summary>
    ///Padge text.
    ///</summary>
    [Parameter]
    public string? Padge { get; set; }

    ///<summary>
    ///The number of padge stamps.
    ///</summary>
    [Parameter]
    public int PadgeCount { get; set; }

    /// <summary>
    /// Notified when number of padge stamps is updated.
    /// </summary>
    [Parameter]
    public EventCallback<int> PadgeCountChanged { get; set; }

    ///<summary>
    ///Padge type.
    ///</summary>
    [Parameter]
    public TraceLevel PadgeType { get; set; } = TraceLevel.Error;

    public string GetPadgeClass()
    {
        string str = "position-absolute top-0 start-100 translate-middle badge rounded-pill ";
        switch (PadgeType)
        {
            case TraceLevel.Error:
                str += "bg-danger";
                break;
            case TraceLevel.Warning:
                str += "bg-warning";
                break;
            case TraceLevel.Info:
                str += "bg-info";
                break;
            default:
                str += "bg-light";
                break;
        }
        return str;
    }

    /// <summary>
    /// Page index number.
    /// </summary>
    /// <remarks>
    /// This is for internal use.
    /// </remarks>
    internal int Index { get; set; }

    /// <summary>
    /// Page identifier.
    /// </summary>
    /// <remarks>
    /// This is for internal use.
    /// </remarks>
    internal string? Id { get; set; }

    protected override void OnInitialized()
    {
        if (Parent == null)
        {
            throw new ArgumentNullException(nameof(Parent), "TabPage must exist within a TabControl");
        }
        base.OnInitialized();
        Parent.AddPage(this);
        if (Active)
        {
            Parent.ActivePage = this;
        }
    }

}